/**
 * Created by admin on 2017/10/10.
 */
var toutiao={
    URL:{
        main:"/toutiao/maincon",
        regis:"/toutiao/register"
    },
    timeStamp2String: function(time){
            var datetime = new Date();
            datetime.setTime(time);
            var year = datetime.getFullYear();
            var month = datetime.getMonth() + 1 < 10 ? "0" + (datetime.getMonth() + 1) : datetime.getMonth() + 1;
            var date = datetime.getDate() < 10 ? "0" + datetime.getDate() : datetime.getDate();
            var hour = datetime.getHours()< 10 ? "0" + datetime.getHours() : datetime.getHours();
            var minute = datetime.getMinutes()< 10 ? "0" + datetime.getMinutes() : datetime.getMinutes();
            var second = datetime.getSeconds()< 10 ? "0" + datetime.getSeconds() : datetime.getSeconds();
            return year + "-" + month + "-" + date;
        },
    //主页面部分
    home:function () {
        //ajax同步运行
        $.ajaxSetup({
            async : false
        });
        var result=null;
        $.get(toutiao.URL.main,function (data) {
            if(data.success){
                result=data.data;
            }
        });
        //定义页面内容
        var html="";
        //定义头部内容
        var head="";
        if(result){

            var users=result.users;
            var news=result.news;
            var nowTime="";
            for(var i=0;i<news.length;i++){
                if(news[i]&&users[i]){
                    if(i==0){
                        nowTime=toutiao.timeStamp2String(news[i].createdDate);
                        head='<h3 class="date"><i class="fa icon-calendar"></i> <span>头条资讯 &nbsp;'+nowTime+'</span> </h3>';
                    }else{
                        if(toutiao.timeStamp2String(news[i].createdDate)==nowTime){
                            head="";
                        }else{
                            head='<h3 class="date"><i class="fa icon-calendar"></i> <span>头条资讯 &nbsp;'+toutiao.timeStamp2String(news[i].createdDate)+'</span> </h3>';
                            nowTime=toutiao.timeStamp2String(news[i].createdDate);
                        }
                    }

                    html+=' <div class="container" id="daily"> <div class="jscroll-inner"> <div class="daily"> '+head+'  <div class="posts"> <div class="post"> <div class="votebar"> <button class="click-like up" aria-pressed="false" title="赞同"><i class="vote-arrow"></i><span class="count">'+news[i].likeCount+'</span></button> <button class="click-dislike down" aria-pressed="true" title="反对"><i class="vote-arrow"></i> </button> </div> <div class="content" data-url="http://nowcoder.com/posts/5l3hjr"> <div > <img class="content-img" src="'+news[i].image+'" alt=""> </div> <div class="content-main"> <h3 class="title"> <a target="_blank" rel="external nofollow" href="'+news[i].link+'">'+news[i].title+'</a></h3> <div class="meta">'+news[i].link+' <span> <i class="fa icon-comment"></i> '+news[i].commentCount+' </span> </div> </div> </div> <div class="user-info"> <div class="user-avatar"> <a href="/user/'+users[i].id+'/"><img width="32" class="img-circle" src="'+users[i].headUrl+'"></a></div> </div> <div class="subject-name">来自 <a href="/user/'+users[i].id+'/">'+users[i].name+'</a></div> </div> </div></div> </div></div>';
                }
            }
        }
        $("#main").html(html);

    },
    //主页登录
    login:function () {
        $('.regAndLogin').modal({
            backdrop:"static",
            keyboard:false,
            show:true
        });

    },
    //注册
    regis:function (un,pw) {
        //ajax同步运行
        $.ajaxSetup({
            async : false
        });
        //注册请求
       var msg="";

        $.post(toutiao.URL.regis,{username:un,password:pw},function (result) {
            if(result.success){
                msg=result.data;
            }else{
                msg=result.error;
            }
            alert(msg);
        });
    }
}